/*
 * @(#)MainPanel.java
 * 
 * Copyright(C) 2004 Yujin Kim
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.netexplode.jtunes.client.ui.component;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JPanel;

import com.netexplode.jtunes.model.player.PlayerManager;

/**
 * <code>MainPanel</code> represents .....
 * 
 * @author ykim
 * @version $Revision: 1.9 $
 * @since 0.1
 */
public class MainPanel extends JPanel {

    private final AlbumArtPanel albumArtPanel = new AlbumArtPanel();

    private final LcdPanel lcdPanel = new LcdPanel();

    private final SourceDevicePanel srcPanel = new SourceDevicePanel();

    private final LibraryPanel libraryPanel = new LibraryPanel();

    private static final Component FILLER = Box.createRigidArea(new Dimension(
            12,
            12));

    public MainPanel() {
        super();
        initialize();
    }

    private void initialize() {
        this.setOptions();
        super.setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));

        /*
         * file menu bar
         */

        final FullMenuBar menu = new FullMenuBar();
        menu.setMaximumSize(new Dimension(Integer.MAX_VALUE, 2));
        super.add(menu);

        /*
         * top
         */
        final JPanel top = new JPanel();
        top.setLayout(new BoxLayout(top, BoxLayout.LINE_AXIS));
        top.add(FILLER);
        top.add(new ControllerPanel());
        top.add(lcdPanel);
        top.add(new SearchPanel());
        top.add(new BrowseButton());
        top.add(FILLER);
        top
                .setMaximumSize(new Dimension(Integer.MAX_VALUE, FILLER
                        .getHeight()));
        super.add(top);

        /*
         * middle
         */
        // create left (source device + album art)
        final JPanel left = new JPanel();
        left.setLayout(new BoxLayout(left, BoxLayout.PAGE_AXIS));

        srcPanel.setPreferredSize(new Dimension(200, 100));
        srcPanel.setMaximumSize(new Dimension(300, Integer.MAX_VALUE));
        left.add(srcPanel);

        final JPanel art = new JPanel();
        // albumArtPanel.setSize(new Dimension(100, 100));
        albumArtPanel.setPreferredSize(new Dimension(180, 180));
        albumArtPanel.setMaximumSize(new Dimension(180, 180));
        art.add(albumArtPanel, BorderLayout.CENTER);

        left.add(albumArtPanel);

        // create splitter using left and right
        final JPanel middle = new JPanel();
        middle.setLayout(new BoxLayout(middle, BoxLayout.LINE_AXIS));
        middle.add(left);
        // create right (collection table)
        middle.add(libraryPanel);

        // final JSplitPane middle = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
        // left, right);
        // middle.setLayout(new BoxLayout(middle, BoxLayout.LINE_AXIS));

        // set border
        middle.setBorder(BorderFactory.createCompoundBorder(BorderFactory
                .createTitledBorder(""), BorderFactory.createEmptyBorder(
                1,
                1,
                1,
                1)));
        // set background
        middle.setBackground(Color.GRAY);
        super.add(middle);

        /*
         * bottom
         */
        final JPanel bottom = new JPanel();
        bottom.setLayout(new BoxLayout(bottom, BoxLayout.LINE_AXIS));
        bottom.add(new PlayerOptionBar());
        bottom.add(new CollectionInfoLabel());
        bottom.add(new DeviceControlBar());
        bottom.setMaximumSize(new Dimension(Integer.MAX_VALUE, 2));
        super.add(bottom);

        if (PlayerManager.getInstance().isInitialized()) {
            // jTunes.setLcdPanelInfo();
        }
    }

    private void setOptions() {
        super.setFont(new Font("Verdana", Font.PLAIN, 10));
        super.setAlignmentX(LEFT_ALIGNMENT);
    }

    /* REFRESH METHODS */

    public void refreshLibraryPanel() {
        libraryPanel.refresh();
    }

    public void refreshSharedCollectionPanel() {
        libraryPanel.reloadShared();
    }

    public void refreshLocalCollectionPanel() {
        libraryPanel.reloadLocal();
    }

    public void setAlbumArtPanelVisible(boolean visible) {
        albumArtPanel.setVisible(visible);
    }

    public void setLcdPanelText(String text) {
        lcdPanel.setLcdPanelText(text);
    }

    public void refreshTree() {
        srcPanel.refresh();
    }
}